using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlTools.Utility;

namespace SqlTools.Utility.Windows
{
	public partial class ConnectToDB : Form
	{
		private bool _loadedServers = false;

		protected bool _newConnection = true;

		protected Server _server = null;

		public ConnectToDB()
		{
			InitializeComponent();
		}

		public string DbName
		{
			get
			{
				if ( !string.IsNullOrEmpty(cboDatabase.Text) )
				{
					return cboDatabase.Text;
				}

				return "";
			}
		}

		public string ConnectionString
		{
			get 
			{
				if ( !string.IsNullOrEmpty(cboDatabase.Text) && _server != null )
				{
					if ( chkIntegratedAuth.Checked )
					{
						_server.UseIntegratedAuthentication = true;
					}
					else
					{
						_server.UseIntegratedAuthentication = false;
						_server.Username = txtUsername.Text;
						_server.Password = txtPassword.Text;
					}

					return _server.GetDatabaseConnectionString(cboDatabase.Text);
				}
					
				return "";  
			}
		}

		private void chkIntegratedAuth_CheckedChanged(object sender, EventArgs e)
		{
			_newConnection = true;
			txtUsername.Enabled = !chkIntegratedAuth.Checked;
			lblUsername.Enabled = !chkIntegratedAuth.Checked;
			txtPassword.Enabled = !chkIntegratedAuth.Checked;
			lblPassword.Enabled = !chkIntegratedAuth.Checked;
		}

		private void cboDatabase_DropDown(object sender, EventArgs e)
		{
			if ( _newConnection )
			{
				this.UseWaitCursor = true;
				Application.DoEvents();

				cboDatabase.Items.Clear();

				if ( cboServer.SelectedItem != null )
				{
					_server = cboServer.SelectedItem as Server;
				}
				else if ( !string.IsNullOrEmpty(cboServer.Text) )
				{
					// from servername
					_server = Server.FromFullName(cboServer.Text);
				}

				if ( _server != null )
				{
					List<string> dbs = _server.GetDatabaseList();
					foreach ( string db in dbs )
					{
						cboDatabase.Items.Add(db);
					}
				}

				_newConnection = false;

				this.UseWaitCursor = false;
				Application.DoEvents();
			}
		}

		private void cboServer_DropDown(object sender, EventArgs e)
		{
			this.UseWaitCursor = true;
			Application.DoEvents();

			if ( !_loadedServers )
			{

				cboServer.DisplayMember = "FullName";

				List<Server> servers = Server.GetServerList();

				foreach ( Server server in servers )
				{
					cboServer.Items.Add(server);
				}

				_loadedServers = true;
				ResetConnection();
			}

			this.UseWaitCursor = false;
			Application.DoEvents();
		}

		private void UpdateNewConnection(object sender, EventArgs e)
		{
			ResetConnection();
		}

		private void ResetConnection()
		{
			_newConnection = true;
			_server = null;
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);

			if ( this.DialogResult == DialogResult.OK )
			{
				if ( cboServer.SelectedItem == null && string.IsNullOrEmpty(cboServer.Text) )
				{
					errorProvider.SetIconAlignment(cboServer, ErrorIconAlignment.MiddleRight);
					errorProvider.SetIconPadding(cboServer, 4);
					errorProvider.SetError(cboServer, "Please select a server");

					e.Cancel = true;

					return;
				}
				else
				{
					errorProvider.SetError(cboServer, "");
				}

				if ( string.IsNullOrEmpty(cboDatabase.Text) )
				{
					errorProvider.SetIconAlignment(cboDatabase, ErrorIconAlignment.MiddleRight);
					errorProvider.SetIconPadding(cboDatabase, 4);
					errorProvider.SetError(cboDatabase, "Please select a database");
					e.Cancel = true;
				}
			}
		}
	}
}